uniform vec3 cameraPosition;
varying vec3 normal;
varying vec3 position;

void main()
{
	vec4 specular = vec4(0.0);
	vec4 diffuse;
	vec3 norm = normalize(normal); //Important: after interpolation normal modulus != 1.
	vec3 lightVector = gl_LightSource[0].position.xyz - position;
	float dist = length(lightVector);
	float attenuation = 0.3; 
	lightVector = normalize(lightVector);
	float nxDir = max(0.0, dot(norm, lightVector));
	diffuse = gl_LightSource[0].diffuse * nxDir * attenuation + 0.3;
	if(nxDir != 0.0)
	{
		vec3 cameraVector = normalize(cameraPosition - position.xyz);
		vec3 halfVector = normalize(lightVector + cameraVector);
		float nxHalf = max(0.0,dot(norm, halfVector));
		float specularPower = pow(nxHalf, 10.0);
		specular = gl_LightSource[0].specular * specularPower * attenuation * 2.0;
	}
	
	gl_FragColor = (diffuse * vec4(1.0, 0.5, 0.7, 1.0)) + 
			(specular);
}
